package cn.liccshop.sys.entity;

import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LiCC
 * @program LiCCShop
 * @description 登录时保存在redis的信息
 * @since 2019-04-07-10:06
 */
@Data
@Accessors(chain = true)
public class UserElement {

    private String userId;

    private String mobile;

    private String token;

    /** 客户端用户标识 **/
    private String platform;

    /** 对用户单独消息推送的ID **/
    private String pushUserId;

    /** 对用户所在群组推送消息的ID **/
    private String pushChannelId;

    /**
     * 对象转map
     * @return
     */
    public Map<String, String> toMap(){
        Map<String, String> map = new HashMap<>();
        map.put("userId", this.userId);
        if (StringUtils.isNotBlank(this.mobile)){
            map.put("mobile", this.mobile);
        }
        if (StringUtils.isNotBlank(this.platform)){
            map.put("platform", this.platform);
        }
        if (StringUtils.isNotBlank(this.pushUserId)){
            map.put("pushUserId", this.pushUserId);
        }
        if (StringUtils.isNotBlank(this.pushChannelId)){
            map.put("pushChannelId", this.pushChannelId);
        }
        return map;
    }

    /**
     * map转对象
     * @param map
     * @return
     */
    public static UserElement fromMap(Map<String, String> map){
        UserElement userElement = new UserElement();
        userElement.setUserId(map.get("userId"));
        userElement.setMobile(map.get("mobile"));
        userElement.setPlatform(map.get("platform"));
        userElement.setPushUserId(map.get("pushUserId"));
        userElement.setPushChannelId(map.get("pushChannelId"));
        return userElement;
    }
}
